#ifndef _MUNIX_RBUFF_H_
#define _MUNIX_RBUFF_H_
/******************************************************************************
** Name: rbuff.h                                                             **
** Description: A ring dual, multi-level ring buffer for storing data        **
**              from USART, network, SPI, etc.                               **
**                                                                           **
** Open Source Initiative (OSI) Approved License                             **
**                                                                           **
**  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
**  All rights reserved.                                                     **
**                                                                           **
**  Redistribution and use in source and binary forms, with or without       **
**  modification, are permitted provided that the following conditions are   **
**  met:                                                                     **
**                                                                           **
**  * Redistributions of source code must retain the above copyright notice, **
**    this list of conditions and the following disclaimer.                  **
**  * Redistributions in binary form must reproduce the above copyright      **
**    notice, this list of conditions and the following disclaimer in the    **
**    documentation and/or other materials provided with the distribution.   **
**                                                                           **
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
**                                                                           **
******************************************************************************/
// $Id: rbuff.h 7 2010-07-14 12:58:31Z nevdull69 $
// $Id: rbuff.h 7 2010-07-14 12:58:31Z nevdull69 $

#include "common.h"
#include <string.h>


#define DEFAULT_RING_BUFF_SZ	16	// power of 2!!

//! @class RingBuff
//! @brief Implements a queue or circular buffer
class RingBuff
{
public:
	RingBuff(uint8 sz);
        RingBuff(const RingBuff &);
	~RingBuff() { delete [] _buff;}

	void		Put(unsigned char) volatile;
	unsigned char	Get() volatile;
	inline uint8	Empty() const { return (_head == _tail); }
	inline uint8	Size() const { return _sz; }
	inline uint8	Length() const { return _len; }
	void		CopyBuff(char * dest) const;
	inline char *	Buff() { return (char*)_buff; }
	inline void	Clear() { memset((char*)_buff,0,DEFAULT_RING_BUFF_SZ); _head=_tail=0; }
        inline volatile uint8 Head() const { return _head; }
        inline volatile uint8 Tail() const { return _tail; }

private:
	volatile byte *_buff;
	uint8		_sz;
	volatile uint8	_head;
	volatile uint8	_tail;
	volatile uint8	_len;
};




#endif	/* _MUNIX_RBUFF_H_ */


